package bme.andraskindler.treenventory.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

/**
 * Egy iránytűt testesít meg.
 * 
 * @author andraskindler
 * 
 */
public class CompassView extends View {

	private Paint mPaint;
	private Path mPathTriangle;
	private float mAzimuth, mPivotX, mPivotY;

	public CompassView(Context context) {
		super(context);
		init();
	}

	public CompassView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public CompassView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		mPathTriangle = new Path();
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setStyle(Paint.Style.FILL);
		mAzimuth = 0f;
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
		mPathTriangle.moveTo((3 * getWidth() / 8), (5 * getHeight() / 6));
		mPathTriangle.lineTo((getWidth() / 2), (getHeight() / 6));
		mPathTriangle.lineTo((5 * getWidth() / 8), (5 * getHeight() / 6));
		mPathTriangle.close();

		mPivotX = getWidth() / 2;
		mPivotY = getHeight() / 2;

		super.onLayout(changed, left, top, right, bottom);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.rotate(mAzimuth, mPivotX, mPivotY);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeWidth(3);
		mPaint.setColor(Color.BLACK);
		canvas.drawCircle(mPivotX, mPivotY, mPivotX - 3, mPaint);
		mPaint.setStyle(Paint.Style.FILL);
		mPaint.setColor(Color.RED);
		canvas.drawPath(mPathTriangle, mPaint);
		super.onDraw(canvas);
	}

	/**
	 * Ezen a függvények keresztül kapja meg a forgásszög-adatokat.
	 * 
	 * @param azimuth
	 */
	public void dataChanged(final float azimuth) {
		mAzimuth = azimuth;
		invalidate();
	}

}
